/*
 * BanBu_FriendTypeListViewController
 * @description 本文件提供从各来源添加好友的controller
 * @package
 * @author 		yinlinlin
 * @copyright
 * @version 		***
 * @description 本文件提供从各来源添加好友的controller
 */
#import "BanBu_FriendTypeListViewController.h"
#import "BanBu_AddNewFriendViewController.h"
#import "BanBu_FriendTypeListCell.h"
#import "BanBu_InviteFriendViewController.h"
#import "BanBu_addTongxunluController.h"
#import "BanBu_UMengShareManager.h"
#import "AppDataManager.h"
#import "BanBu_PersonCardController.h"
#import "UMSocial.h"
#import "BanBu_addTXLController.h"
@interface BanBu_FriendTypeListViewController ()

@end

@implementation BanBu_FriendTypeListViewController
{
    //获取头像图片
    UIImageView * _iconImg;
}

- (void)dealloc
{
    [_typeListOfzh release];
    _typeListOfzh = nil;
    [_typelistOfOther release];
    _typelistOfOther = nil;
    [_table release];
    _table = nil;
    [_iconImg release];
    _iconImg = nil;
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _iconImg = [[UIImageView alloc]initWithFrame:CGRectMake(3, 63, 53, 53)];
    NSDictionary *profile = [NSDictionary dictionaryWithDictionary:[UserDefaults valueForKey:MyAppDataManager.useruid]];
    [_iconImg setImageWithURL:[NSURL URLWithString:[profile valueForKey:@"uface"]] placeholderImage:[UIImage imageNamed:@"default.png"] andType:0];
    
    self.title = NSLocalizedString(@"andNewFriendTitle", nil);
    // Do any additional setup after loading the view.
    _typeListOfzh = [[NSArray alloc] initWithObjects:@"add_tongxunlu",@"add_wechat",@"add_QQ",@"add_weibo",@"add_tencent", nil];
    _typelistNamezh = [[NSArray alloc] initWithObjects:@"add_tongxunlu",@"添加微信好友",@"添加QQ好友",@"添加新浪微博",@"添加腾讯微博", nil];
    _typelistOfOther = [[NSArray alloc] initWithObjects:@"add_tongxunlu",@"add_facebook",@"add_twitter", nil];
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, __MainScreen_Width, __MainScreen_Height - 44) style:UITableViewStylePlain];
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_table];
    self.view.backgroundColor = UIColorWithRGB(242, 243, 232, 1.0);
    _table.backgroundColor = UIColorWithRGB(242, 243, 232, 1.0);
    _table.dataSource = self;
    _table.delegate = self;
    if (![UserDefaults valueForKey:@"user_shortUrl"])
    {
        [self getShortUrlForUser_id];
    }
    
}

#pragma mark - 获取用户id网页个人空间的短网址
- (void)getShortUrlForUser_id
{
    NSString * shareUrl = [NSString stringWithFormat:@"http://www.halfarea.com/shareto/userview.html?userid=%@",MyAppDataManager.useruid];
    [AppComManager getBanBuData:Banbu_GetShortUrl par:[NSDictionary dictionaryWithObjectsAndKeys:shareUrl,@"url", nil] delegate:self];
}

#pragma mark - BanBuRequetDelegate
- (void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error
{
    NSLog(@"%@",resDic);
    if([AppComManager respondsDic:resDic isFunctionData:Banbu_GetShortUrl])
    {
        if([[resDic valueForKey:@"ok"] boolValue])
        {
            [UserDefaults setValue:[resDic valueForKey:@"shorturl"] forKey:@"user_shortUrl"];
        }
    }
    
}
#pragma mark - UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //    return (section == 0)?1:5;
    if (section == 0)
    {
        return 1;
    }
    NSString *langauage=[MyAppDataManager getPreferredLanguage];
    if([langauage isEqual:@"zh-Hans"])
    {
        return 5;
    }
    return 3;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identify = @"cellIdentify";
    BanBu_FriendTypeListCell * cell = (BanBu_FriendTypeListCell *)[tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell)
    {
        cell = [[[BanBu_FriendTypeListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify]autorelease];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell.imageView setFrame:CGRectMake(13, 8, 26, 26)];
    //添加半圆好友
    if (indexPath.section == 0)
    {
        cell.typeLa.text = NSLocalizedString(@"add_halfarea", nil);
        NSString * imagePath = [[NSBundle mainBundle] pathForResource:@"add_halfarea" ofType:@"png"];
        cell.typeImgV.image = [UIImage imageWithContentsOfFile:imagePath];
        [cell.lineImgV setFrame:CGRectMake(0, 43.5, 320, 0.5)];
        [cell.prelineImgV setHidden:NO];
    }
    //添加其他好友
    else
    {
        //当前系统语言是汉语
        NSString *langauage=[MyAppDataManager getPreferredLanguage];
        if([langauage isEqual:@"zh-Hans"])
        {
            if (indexPath.row == 4)
            {
                [cell.lineImgV setFrame:CGRectMake(0, 43.5, 320, 0.5)];
            }
            else
            {
                [cell.lineImgV setFrame:CGRectMake(40, 43.5, 300, 0.5)];
            }
            if (indexPath.row == 0)
            {
                [cell.prelineImgV setHidden:NO];
                cell.typeLa.text =  NSLocalizedString([_typelistNamezh objectAtIndex:0], nil);
            }
            else
            {
                [cell.prelineImgV setHidden:YES];
                cell.typeLa.text = [_typelistNamezh objectAtIndex:indexPath.row];
            }
            NSString * imagePath = [[NSBundle mainBundle] pathForResource:[_typeListOfzh objectAtIndex:indexPath.row] ofType:@"png"];
            cell.typeImgV.image = [UIImage imageWithContentsOfFile:imagePath];
        }
        //其他语言邀请facebook和twitter
        else
        {
            if (indexPath.row == 2)
            {
                [cell.lineImgV setFrame:CGRectMake(0, 43.5, 320, 0.5)];
            }
            else
            {
                [cell.lineImgV setFrame:CGRectMake(40, 43.5, 300, 0.5)];
            }
            if (indexPath.row == 0)
            {
                [cell.prelineImgV setHidden:NO];
                cell.typeLa.text =   NSLocalizedString(@"add_tongxunlu", nil);
            }
            else
            {
                [cell.prelineImgV setHidden:YES];
                if (indexPath.row == 1)
                {
                    cell.typeLa.text = @"Facebook";
                }
                else
                {
                    cell.typeLa.text = @"Twitter";
                }
            }
            NSString * imagePath = [[NSBundle mainBundle] pathForResource:[_typelistOfOther objectAtIndex:indexPath.row] ofType:@"png"];
            cell.typeImgV.image = [UIImage imageWithContentsOfFile:imagePath];
        }
        
        
        
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        BanBu_AddNewFriendViewController *addNew = [[BanBu_AddNewFriendViewController alloc] init];
        //        [self.leveyTabBarController hidesTabBar:YES animated:YES];
        [self.navigationController pushViewController:addNew animated:YES];
        [addNew release];
        return;
        
    }
    //汉语
    NSString *langauage=[MyAppDataManager getPreferredLanguage];
    if([langauage isEqual:@"zh-Hans"])
    {
        switch (indexPath.row)
        {
                //邀请通讯录好友
            case 0:
            {
                BanBu_addTXLController * tongxunlu = [[BanBu_addTXLController alloc]init];
                [self.navigationController pushViewController:tongxunlu animated:YES];
                [tongxunlu release];
                tongxunlu = nil;
                break;
            }
                //邀请微信好友
            case 1:
            {
                UIActionSheet * sheet = [[UIActionSheet alloc]initWithTitle:@"添加微信好友" delegate:self cancelButtonTitle:NSLocalizedString(@"cancelNotice", nil) destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"wechatSessionPlatform", nil),NSLocalizedString(@"wechatTimelinePlatform", nil), nil];
                [sheet showInView:self.view];
                [sheet release];
                sheet = nil;
                break;
            }
                //邀请QQ好友
            case 2:
            {
                NSDictionary *profile = [NSDictionary dictionaryWithDictionary:[UserDefaults valueForKey:MyAppDataManager.useruid]];
                UIImage * shareImg = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[profile valueForKey:@"uface"]]]];
                NSString * shareUrl;
                if ([UserDefaults valueForKey:@"user_shortUrl"])
                {
                    shareUrl = (NSString *)[UserDefaults valueForKey:@"user_shortUrl"];
                }
                else
                {
                    shareUrl = [NSString stringWithFormat:@"http://www.halfarea.com/shareto/userview.html?userid=%@",MyAppDataManager.useruid];
                }
                
                NSString * shareText = [NSString stringWithFormat:@"%@ %@。%@ %@",NSLocalizedString(@"invite_pre", nil),MyAppDataManager.useruid,NSLocalizedString(@"koochatComment", nil),shareUrl];
                [[BanBu_UMengShareManager shareUMengManager] shareToPlatform:ShareToQQ WithText:shareText WithImage:shareImg WithClickUrlStr:shareUrl WithPerformVC:self];
                break;
            }
                //邀请新浪微博好友
            case 3:
            {
                BanBu_PersonCardController * card = [[BanBu_PersonCardController alloc]init];
                card.shareType = UMShareToSina;
                
                [self presentViewController:card animated:YES];
                
                return;
            }
                //邀请腾讯微博好友
            default:
            {
                BanBu_PersonCardController * card = [[BanBu_PersonCardController alloc]init];
                card.shareType = UMShareToTencent;
                
                [self presentViewController:card animated:YES];
            }
                break;
        }
    }
    //其他语言
    else
    {
        switch (indexPath.row) {
                //通讯录好友
            case 0:
            {
                BanBu_addTXLController * tongxunlu = [[BanBu_addTXLController alloc]init];
                [self.navigationController pushViewController:tongxunlu animated:YES];
                [tongxunlu release];
                tongxunlu = nil;
                break;
            }
                //FaceBook
            case 1:
            {
                NSDictionary *profile = [NSDictionary dictionaryWithDictionary:[UserDefaults valueForKey:MyAppDataManager.useruid]];
                UIImage * shareImg = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[profile valueForKey:@"uface"]]]];
                NSString * shareUrl;
                if ([UserDefaults valueForKey:@"user_shortUrl"])
                {
                    shareUrl = (NSString *)[UserDefaults valueForKey:@"user_shortUrl"];
                }
                else
                {
                    shareUrl = [NSString stringWithFormat:@"http://www.halfarea.com/shareto/userview.html?userid=%@",MyAppDataManager.useruid];
                }
                
                NSString * shareText = [NSString stringWithFormat:@"%@ %@。%@ %@",NSLocalizedString(@"invite_pre", nil),MyAppDataManager.useruid,NSLocalizedString(@"koochatComment", nil),shareUrl];
                [[BanBu_UMengShareManager shareUMengManager] shareToPlatform:ShareToFacebook WithText:shareText WithImage:shareImg WithClickUrlStr:shareUrl WithPerformVC:self];
                break;
            }
                //Twitter
            case 2:
            {
                NSDictionary *profile = [NSDictionary dictionaryWithDictionary:[UserDefaults valueForKey:MyAppDataManager.useruid]];
                UIImage * shareImg = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[profile valueForKey:@"uface"]]]];
                NSString * shareUrl;
                if ([UserDefaults valueForKey:@"user_shortUrl"])
                {
                    shareUrl = (NSString *)[UserDefaults valueForKey:@"user_shortUrl"];
                }
                else
                {
                    shareUrl = [NSString stringWithFormat:@"http://www.halfarea.com/shareto/userview.html?userid=%@",MyAppDataManager.useruid];
                }
                
                NSString * shareText = [NSString stringWithFormat:@"%@ %@。%@ %@",NSLocalizedString(@"invite_pre", nil),MyAppDataManager.useruid,NSLocalizedString(@"koochatComment", nil),shareUrl];
                [[BanBu_UMengShareManager shareUMengManager] shareToPlatform:ShareToTwitter WithText:shareText WithImage:shareImg WithClickUrlStr:shareUrl WithPerformVC:self];
                break;
            }
            default:
                break;
        }
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headerV = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 20)]autorelease];
    headerV.backgroundColor = [UIColor clearColor];
    return headerV;
    
}


#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == actionSheet.cancelButtonIndex)
    {
        return;
    }
    NSString * shareUrl;
    if ([UserDefaults valueForKey:@"user_shortUrl"])
    {
        shareUrl = (NSString *)[UserDefaults valueForKey:@"user_shortUrl"];
    }
    else
    {
        shareUrl = [NSString stringWithFormat:@"http://www.halfarea.com/shareto/userview.html?userid=%@",MyAppDataManager.useruid];
    }
    NSString * shareText = [NSString stringWithFormat:@"%@ %@。\n%@ \n%@",NSLocalizedString(@"invite_pre", nil),MyAppDataManager.useruid,NSLocalizedString(@"koochatComment", nil),shareUrl];
    //微信好友
    if (buttonIndex == 0)
    {
        NSLog(@"微信好友");
        //        NSString * urlPath = [[@"www.baidu.com" dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
        
        
        [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
        //设置微信好友或者朋友圈的分享url,下面是微信好友，微信朋友圈对应wechatTimelineData
        [UMSocialData defaultData].extConfig.wechatSessionData .url = shareUrl;
        [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:shareText image:_iconImg.image location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response){
            if (response.responseCode == UMSResponseCodeSuccess) {
                NSLog(@"分享成功！");
                
            }
        }];
        
    }
    //微信好友圈
    else
    {
        [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
        //设置微信好友或者朋友圈的分享url,下面是微信好友，微信朋友圈对应wechatTimelineData
        [UMSocialData defaultData].extConfig.wechatTimelineData.url = shareUrl;
        
        [[BanBu_UMengShareManager shareUMengManager] shareToPlatform:ShareToWXTimeline WithText:shareText WithImage:_iconImg.image WithClickUrlStr:nil WithPerformVC:self];
        
        return;
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
